$(document).ready(function() {
//	top.UppTabs.operator = top.getCurrentTagTitle();// 设置用户tab的标题值
	doSearch();
});

function doSearch(){
	var s = $('input[name="beginTime"]').val();
	var e = $('input[name="endTime"]').val();
	//if(s == "" || e == "")
	//	return;
	$("#data_list").datagrid({
		onLoadError : function(data) {
			uppAjaxError(data);
		},
		onLoadSuccess:function(data){
			$("#export_link").attr('href',contextPath+'/iccard/report/temp/'+data.fileName);
		},
		onLoadSuccess: function (data) {
			paint();
		},
		queryParams : {
			beginTime : s,
			endTime : e
		},
		columns : [ [ {
			field : 'daliyTotalEarningsId',
			title : '',
			hidden : true
		}, {
			field : 'daliyEarningsDate',
			title : '日期 ',
			align : 'center',
			formatter : function(value, row, index) {
				var d =new Date(Number(value));
				return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
			}
		}, {
			field : 'comboCardRealEarnings',
			title : '套餐卡实际收益总额'
		}, {
			field : 'rechargeableCardRealTotalEarnings',
			title : '充值卡实际收益总额'
		}, {
			field : 'realTotalEarnings',
			title : '实际收益总额'
		}, {
			field : 'daliyWaitAmount',
			title : '待结算总额'
		} ] ],
		autoRowHeight : true,
		fitColumns : true,
		pagination : true,
		nowrap : false,
		singleSelect : true,
		rownumbers : true
	});
	//汇总
	$.ajax({
        url: contextPath + '/iccard/report/financial/income/reportSum',
        dataType: "json",
        data: {
			beginTime : s,
			endTime : e
        },
        success: function( data ) {
        	$("#collect_amount").text("实际收益累积总额 ："+data.incomeTotal+" 待结算累积总额 ："+data.settlementTotal);
        }
      });
	
}

function paint(){
	var data=$("#data_list").datagrid("getRows");
	var days = new Array();
	var totals = new Array();
	var packages = new Array();
	var prepaids = new Array();
	var packages_total =0.0;
	var prepaids_total =0.0;
	for(var i = 0;i<data.length;i++){
		var d =new Date(Number(data[i].daliyEarningsDate));
		days[i] = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
		totals[i] = data[i].realTotalEarnings;
		packages[i] = data[i].comboCardRealEarnings;
		prepaids[i] = data[i].rechargeableCardRealTotalEarnings;
		packages_total+=data[i].comboCardRealEarnings;;
		prepaids_total+= data[i].rechargeableCardRealTotalEarnings;
	}
    //
    $('#income_chart').highcharts({
        chart: {
            type: 'line'
        },
        title: {
            text: '收益额统计曲线图'
        },
        subtitle: {
            text: '('+days[0]+'至'+days[days.length-1]+')'
        },
        xAxis: {
            categories: days
        },
        yAxis: {
            title: {
                text: '金额（元）'
            }
        },
        tooltip: {
            enabled: true,
            formatter: function() {
                return '<b>'+ this.series.name +'</b><br/>'+
                    this.x +': '+ this.y +'元';
            }
        },
        plotOptions: {
            line: {
                dataLabels: {
                    enabled: true
                },
                enableMouseTracking: true
            }
        },
        series: [{
            name: '总额',
            data: totals
        }, {
            name: '套餐卡收益额',
            data: packages
        },{
            name: '购物卡收益额',
            data: prepaids
        }]
    });
//
    //饼图
    $('#income_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '收益额统计总占比图'
        },
        subtitle: {
            text: '('+days[0]+'至'+days[days.length-1]+')'
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: [
                ['购物卡收益',   100*prepaids_total/(prepaids_total+packages_total)],
                ['套餐卡收益',   100*packages_total/(prepaids_total+packages_total)]
            ]
        }]
    });
}